package com.asw.springboot.common.fastdfs;

import com.asw.springboot.utils.StringUtil;

import org.apache.log4j.Logger;
import org.csource.upload.UploadFileUtil;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by anpushang on 2016/3/17.
 * fastdfs 文件上传工具类
 */
public class FastDfsUtil {

    private static final Logger logger = Logger.getLogger(FastDfsUtil.class);

    /***
     * 批量上传 ,返回上传之后的url集合
     * @param request
     * @return
     */
    public static List<String> fastUploadFileArray(HttpServletRequest request) {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        List<String> imageList = new ArrayList<String>();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            try {
                MultipartFile multipartFile = entity.getValue();
                String fileName = multipartFile.getOriginalFilename();
                String fileSuffix = StringUtil.getFileExt2(fileName);
                fileSuffix = fileSuffix == null ? "png" : fileSuffix;
                String myFileUrl = UploadFileUtil.uploadFile(multipartFile.getInputStream(), fileSuffix, fileName);
                imageList.add(myFileUrl);
            } catch (Exception e) {
                logger.error("图片上传失败",e);
                e.printStackTrace();
            }
        }
        return imageList;
    }

    /***
     * 批量上传 ,返回上传之后的url集合
     * @param multipartFileArray
     * @return
     */
    @SuppressWarnings("all")
    public static List<String> fastUploadFileArray(MultipartFile[] multipartFileArray) {
        List<String> imageList = new ArrayList<String>();
        if (multipartFileArray != null && multipartFileArray.length > 0) {
            Arrays.asList(multipartFileArray).parallelStream().forEach(multipartFile -> imageList.add(getFastUploadFileUrl(multipartFile)));
        } else {
            logger.info("MultipartFile对象为null...");
        }
        return imageList;
    }


    public static String getFastUploadFileUrl(MultipartFile multipartFile) {
        String myFileUrl = null;
        try {
            String fileName = multipartFile.getOriginalFilename();
            myFileUrl = UploadFileUtil.uploadFile(multipartFile.getInputStream(), "jpg", fileName);
            return myFileUrl;
        } catch (Exception e) {
            logger.error("图片上传失败", e);
            e.printStackTrace();
        }
        return myFileUrl;
    }


    /***
     * 富文本html文件代码上传到服务器
     * @param ueditorHtml
     * @return
     */
    public static String fastUploadFile(String ueditorHtml) {
        byte[] ueditorHtmlBytes = ueditorHtml.getBytes();
        String resultUrl = null;
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(ueditorHtmlBytes);
            resultUrl = UploadFileUtil.uploadFile(stream, null, null);
            logger.info("图文详情上传成功！！！" + resultUrl);
        } catch (Exception e) {
            logger.error("图文详情上传失败！！！" + e.getMessage(), e);
            e.printStackTrace();
        }
        return resultUrl;
    }


    public static String fastMigrateImage(String urlString) {
        String myFileUrl = null;
        try {
            // 构造URL
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            InputStream inputStream = con.getInputStream();

            myFileUrl = UploadFileUtil.uploadFile(inputStream, "jpg", null);
        } catch (Exception e) {
            logger.error("图片上传失败", e);
            e.printStackTrace();
        }
        return myFileUrl;
    }


    /***
     * 单张图片上传
     * @param multipartFile
     * @return
     */
    public static String fastUploadFile(MultipartFile multipartFile) {
        String myFileUrl = null;
        if (multipartFile != null && multipartFile.getSize() > 0) {
            myFileUrl = getFastUploadFileUrl(multipartFile);
        } else {
            logger.info("MultipartFile对象为null...");
        }
        return myFileUrl;
    }

    /***
     * 单张图片流上传
     * @param is
     * @return
     */
    public static String fastUploadFile(InputStream is) {
        String myFileUrl = null;
        try {
            if (is != null) {
                myFileUrl = UploadFileUtil.uploadFile(is, "jpg", null);
            } else {
                logger.info("MultipartFile对象为null...");
            }
        } catch (Exception e) {
            logger.error("图片上传失败", e);
            e.printStackTrace();
        }
        return myFileUrl;
    }


    /***
     * 文件回显
     * @param fileUrl
     * @return
     */
    public static String FastDownloadFile(String fileUrl) {
        return UploadFileUtil.DownloadFile(fileUrl);
    }
}
